package com.enba.mallapi.module.shopping.controller;

import com.enba.boot.core.base.Result;
import com.enba.mall.core.shopping.req.AddToCartRequest;
import com.enba.mall.core.shopping.req.DeleteCartItemsRequest;
import com.enba.mall.core.shopping.req.SelectAllRequest;
import com.enba.mall.core.shopping.req.UpdateQuantityRequest;
import com.enba.mall.core.shopping.resp.ShoppingCartResp.ShoppingCartItemResp;
import com.enba.mall.core.shopping.resp.SkuByItemIdResp;
import com.enba.mall.core.shopping.service.ShoppingCartService;
import com.enba.mallapi.annotation.CurrentUser;
import com.enba.mallapi.annotation.DistributedLock;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Set;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "购物车管理")
@RestController
@RequestMapping("/api/cart")
public class ShoppingCartController {

  private final ShoppingCartService shoppingCartService;

  public ShoppingCartController(ShoppingCartService shoppingCartService) {
    this.shoppingCartService = shoppingCartService;
  }

  /** 获取购物车列表 */
  @GetMapping("/list")
  @ApiOperation(value = "获取购物车列表")
  public Result<List<ShoppingCartItemResp>> getCartList(@CurrentUser Long userId) {
    return Result.success(shoppingCartService.getCartList(userId));
  }

  /** 添加商品到购物车 */
  @PostMapping("/add")
  @ApiOperation(value = "添加商品到购物车")
  @DistributedLock
  public Result<Boolean> addToCart(
      @RequestBody @Validated AddToCartRequest request, @CurrentUser Long userId) {
    return Result.success(
        shoppingCartService.addToCart(
            userId, request.getSpuId(), request.getSkuId(), request.getQuantity()));
  }

  /** 更新购物车商品数量 */
  @PutMapping("/update-quantity")
  @ApiOperation(value = "更新购物车商品数量")
  @DistributedLock
  public Result<Boolean> updateCartItemQuantity(
      @RequestBody @Validated UpdateQuantityRequest request, @CurrentUser Long userId) {
    return Result.success(
        shoppingCartService.updateCartItemQuantity(
            userId, request.getItemId(), request.getQuantity()));
  }

  /** 删除购物车商品 */
  @DeleteMapping("/delete")
  @ApiOperation(value = "删除购物车商品")
  @DistributedLock
  public Result<Boolean> deleteCartItems(
      @RequestBody @Validated DeleteCartItemsRequest request, @CurrentUser Long userId) {
    return Result.success(shoppingCartService.deleteCartItems(userId, request.getItemIds()));
  }

  /** 全选/取消全选购物车商品 */
  @PutMapping("/select-all")
  @ApiOperation(value = "全选/取消全选购物车商品")
  public Result<Boolean> selectOrUnselectAll(
      @RequestBody @Validated SelectAllRequest request, @CurrentUser Long userId) {
    return Result.success(shoppingCartService.selectOrUnselectAll(userId, request.isSelected()));
  }

  @PutMapping("/select-unselect")
  @ApiOperation(value = "勾选取消商品")
  public Result<Boolean> selectOrUnselect(
      @RequestBody @Validated SelectAllRequest request, @CurrentUser Long userId) {
    return Result.success(shoppingCartService.selectOrUnselect(request, userId));
  }

  // 根据购物车项获取商品信息
  @PostMapping("/get-product-info")
  @ApiOperation(value = "根据购物车项获取商品信息")
  public Result<List<SkuByItemIdResp>> getProductInfo(
      @RequestBody Set<Long> itemIds, @CurrentUser Long userId) {
    return Result.success(shoppingCartService.getProductInfo(itemIds, userId));
  }
}
